package net.grandnature.android.morse.player;

import java.io.IOException;
import java.util.ArrayList;

public class MorseStringBuilder {
	private ArrayList<MorsePlayer> players;
	
	public MorseStringBuilder(){
		this.players = new ArrayList<MorsePlayer>();
	}
	public void addPlayer(MorsePlayer player){
		this.players.add(player);
	}
	
	public void morsePlay(String str){
		char[] cs = str.toCharArray();
		for (char c : cs) {
			if(c=='1'){
				for( MorsePlayer player: this.players){
					player.shorSignal();
					player.silent(1);
				}
			}else if(c=='3'){
				for( MorsePlayer player: this.players){
					player.longSignal();
					player.silent(1);
				}
			}else if(c=='/'){
				for( MorsePlayer player: this.players){
					player.silent(2);
				}
			}else if(c==' '){
				for( MorsePlayer player: this.players){
					player.silent(6);
				}

			}else{
				// 未知の文字は無視
			}
		}
	}
	public void close() {
		for( MorsePlayer player: this.players){
			try {
				player.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				throw new RuntimeException(e);
			}
		}
	}
}
